package com.zx.cloud.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.zx.cloud.ApiVersion;
import com.zx.cloud.common.auth0.jwt.Token;
import com.zx.cloud.common.filter.CurrentUserId;
import com.zx.cloud.common.filter.IgnoreAuth;
import com.zx.cloud.common.filter.MagicConstants;
import com.zx.cloud.controller.client.LoginClient;
import com.zx.cloud.domain.User;
import com.zx.cloud.model.request.ManagerLoginRequest;
import com.zx.cloud.model.response.ObjectDataResponse;
import com.zx.cloud.model.response.Response;
import com.zx.cloud.util.CookieManager;


@RestController
@RequestMapping(value = ApiVersion.MANAGER_V1_PREFIX + "/user")
public class LoginController {
	
	@Autowired
	private LoginClient loginClient;
	
	@IgnoreAuth
	@ApiOperation(value = "管理系统用户登录" , notes = "管理系统用户登录")
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Token> login(HttpServletRequest request,HttpServletResponse response,@RequestBody ManagerLoginRequest loginRequest){
		ObjectDataResponse<Token> loginRequestObj = loginClient.login(loginRequest);
		new CookieManager(request, response).setCookie(MagicConstants.AUTH_TOKEN, loginRequestObj.getData().getToken(), 7*24*60*60);
		return loginRequestObj;
	}
	
	@ApiOperation(value = "登录用户-主页面提示信息" , notes = "登录用户-主页面提示信息")
	@RequestMapping(value = "/loginFind", method = RequestMethod.POST)
	@ResponseBody
	public Response loginFind(@RequestBody User user){
		ObjectDataResponse<User>  loginRequestObj = loginClient.loginFind(user);
		return loginRequestObj;
	}
	
	@ApiOperation(value = "登录用户-修改密码" , notes = "登录用户-修改密码")
	@RequestMapping(value = "/updatePassWord", method = RequestMethod.POST)
	@ResponseBody
	public Response updatePassWord(@CurrentUserId Long userId, @RequestBody User user){
		ObjectDataResponse<Integer>  loginRequestObj = loginClient.updatePassWord(user);
		return loginRequestObj;
	}
	
	@ApiOperation(value = "用户管理-首页统计数据信息", notes = "用户管理-首页统计数据信息")
	@RequestMapping(value = "/statisticalInformation", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse <Map<String, Object>> statisticalInformation(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId){
		
		ObjectDataResponse <Map<String, Object>> map = loginClient.statisticalInformation();
		
		return map;
	}
}
